//
//  PlaceCollectionViewCell.m
//  GOGOTravel
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "PlaceCollectionViewCell.h"
#import "GiFHUD.h"
@implementation PlaceCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self handleData];
        [self initTableView];
        self.allCity = [NSMutableArray array];
        [GiFHUD setGifWithImageName:@"b879f2e742d94b2a8f2362d0ca7e18c7.gif"];
        [GiFHUD show];
    }
    return self;
}

- (void)dealloc{
    [_tableView release];
    [_indexPath release];
    [_oversea_cityArr release];
    [_domestic_cityArr release];
    [super dealloc];
}



/**<  创建tableView */

- (void)initTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 25, self.frame.size.width, self.frame.size.height - 30) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    _tableView.alpha = 0.9;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableView];
    [_tableView release];
  
    
    //注册
    [_tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"BaseTableViewCell1"];
     [_tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"BaseTableViewCell2"];
     [_tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"BaseTableViewCell3"];
    [_tableView registerNib:[UINib nibWithNibName:@"PlaveTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"PlaveTableViewCell"];
    
}
/**<  代理方法 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        return 80;
    }
    return 30;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.indexPath.row == 0) {
        Place *model = _domestic_cityArr[0];
        return model.all_city_listArr.count + 3;
    }else{
        Place *model = _oversea_cityArr[0];
        return model.all_city_listArr.count + 3;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.indexPath.row == 0) {
        
        Place *model = _domestic_cityArr[0];
        if (indexPath.row == 0) {
            BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell1"];
            cell.textLabel.text = @"GPS 定位失败";
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.userInteractionEnabled = NO;
            return cell;
        }else if (indexPath.row == 1) {
            PlaveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlaveTableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
            cell.userInteractionEnabled = NO;
            return cell;
        }else if (indexPath.row == 2){
            BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell2"];
            cell.textLabel.text = @"【全部城市】";
            cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.textColor = [UIColor whiteColor];
            
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.userInteractionEnabled = NO;
            return cell;
        } else {
            
            BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell3"];
            
            AllCityList *model1 = model.all_city_listArr[indexPath.row - 3];
            cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.font = [UIFont systemFontOfSize:12];
            cell.textLabel.text = model1.name;
            return cell;
        }

    }else{
        Place *model = _oversea_cityArr[0];
        if (indexPath.row == 0) {
            BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell1"];
            cell.textLabel.text = @"GPS 定位失败";
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
                        cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else if (indexPath.row == 1) {
            PlaveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlaveTableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
            return cell;
        }else if (indexPath.row == 2){
            BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell2"];
            cell.textLabel.text = @"【全部城市】";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            return cell;
        } else {
            
            BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell3"];
            cell.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
            AllCityList *model1 = model.all_city_listArr[indexPath.row - 3];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.font = [UIFont systemFontOfSize:12];
            cell.textLabel.text = model1.name;
            return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
    
    [self.delegate JumpPage:cell.textLabel.text];
   
    NSLog(@"00000%@", cell.textLabel.text);
}
/**<  数据处理 */
- (void)handleData{
    
    [AFNetWorkingHandle GETRequestWithURLString:@"http://api.breadtrip.com/hunter/products/v2/metadata/?city_name=%E5%A4%A7%E8%BF%9E&sign=f598ec442a3f5cf735e4a671f1e14993&with_citydata=true&with_sortdata=true" completionWithSuccess:^(id result) {
        
        NSDictionary *cityData = result[@"city_data"];
        NSDictionary *oversea_city = cityData[@"oversea_city"];
        NSDictionary *domestic_city = cityData[@"domestic_city"];
       
        NSArray *arr = oversea_city[@"all_city_listArr"];
        NSArray *arr1 = domestic_city[@"all_city_listArr"];
        
        for (NSString *city in arr) {
            [_allCity addObject:city];
        }
        
        for (NSString *city in arr1) {
            [_allCity addObject:city];
        }
        
        Place *model = [[Place alloc] initWithDic:oversea_city];
        self.oversea_cityArr  = [NSMutableArray array];
        [_oversea_cityArr addObject:model];
        
        
        Place *model1 = [[Place alloc] initWithDic:domestic_city];
        self.domestic_cityArr = [NSMutableArray array];
        [_domestic_cityArr addObject:model1];
       
        
        
        
        [_tableView reloadData];
        
        [GiFHUD dismiss];
        
    } withFailure:^(id error) {
        NSLog(@"%@", error);
    }];
}

@end
